CC=gcc
CXX=g++
CFLAGS=-Wall $(DEBUG) $(DEFINITIONS) $(CINCLUDES) $(CMISCFLAGS)
CPPFLAGS=-Wall -ansi $(DEBUG) $(EXCEPTIONS) $(CPPINCLUDES) $(DEFINITIONS) $(CPPMISCFLAGS)
DEBUG=-O3 -g3
CMISCFLAGS=
CPPMISCFLAGS=-DGIMI_CLIENT_API=10303
DEFINITIONS=-DLINUX_OS -DGIMI_NO_DATATYPE_INCLUDES
EXCEPTIONS=-fexceptions -ffast-math 
CINCLUDES=-I. -I../../scanmatch -I../../mcl -I$(GDIR)utils
CPPINCLUDES=-I. -I../../scanmatch -I../../mcl $(OCCU_INCLUDE) -I$(GDIR)glutImageGUI -I$(GDIR)utils \
	-I$(GDIR)JPEGEncoder

LD=g++
LDFLAGS=
LIBS=-lrt -lpthread -lGL -lGLU -lglut -ljpeg

GDIR=../../../../
GIMDIR=../../../..
SRCSC=
SRCSCPP=j2b2.cpp $(OCCU_SRC_CPP)
VPATH=
CPPGIMI=
CGIMI=

CSRCUTIL = $(GIMDIR)/utils/ownutils.c $(GIMDIR)/utils/owndebug.c
CPPIMAGEGUI = $(GIMDIR)/glutImageGUI/glutImageGUI.cpp $(GIMDIR)/JPEGEncoder/JPEGEncoder.cpp

SRC_LOG=convert_log.cpp $(OCCU_SRC_CPP)  $(CPPMCL)
CSRC_LOG=$(CSRCUTIL)

SRC_OCCU_SLAM=occu_slam.cpp $(CPPSM) $(CPPIMAGEGUI) $(OCCU_SRC_CPP)
CSRC_OCCU_SLAM=$(CSRCUTIL)

SRC_LASER_ODO=laser_odo.cpp $(CPPSM) $(CPPIMAGEGUI) $(OCCU_SRC_CPP)
CSRC_LASER_ODO=$(CSRCUTIL)

SRC_OCCU_MCL=occu_mcl.cpp $(CPPSM) $(CPPMCL) $(CPPIMAGEGUI) $(OCCU_SRC_CPP)
CSRC_OCCU_MCL=$(CSRCUTIL)

SRC_OCCU_SLAMCL=occu_slamcl.cpp $(CPPSM) $(CPPMCL) $(CPPIMAGEGUI) $(OCCU_SRC_CPP)
CSRC_OCCU_SLAMCL=$(CSRCUTIL)

SRC_TOPO=topo.cpp $(CPPSM) $(CPPMCL) $(CPPIMAGEGUI) $(OCCU_SRC_CPP)
CSRC_TOPO=$(CSRCUTIL)

SRC_PLOT_DR=plot_dr.cpp $(CPPSM) $(CPPMCL) $(CPPIMAGEGUI) $(OCCU_SRC_CPP)
CSRC_PLOT_DR=$(CSRCUTIL)

SRC_OCCU_J2B2_TEST=occu_j2b2_test.cpp $(CPPSM) $(CPPMCL) $(CPPIMAGEGUI) $(OCCU_SRC_CPP)
CSRC_OCCU_J2B2_TEST=$(CSRCUTIL)

SRC_TIMING_TEST=timing_test.cpp $(CPPSM) $(CPPMCL) $(CPPIMAGEGUI) $(OCCU_SRC_CPP)
CSRC_TIMING_TEST=$(CSRCUTIL)


## Build GIMI and it's dependencies.
## Files are set to CGIMI and CPPGIMI variables
include Makefile.mcl
include $(GDIR)grid/Makefile.occu 

CGIMI += $(CMCL)
CPPGIMI += $(CPPMCL) $(CPPSM)

## Construct final lists of CPP and C objects
OBJSC=$(SRCSC:.c=.o) $(CGIMI:.c=.o)
OBJSCPP=$(SRCSCPP:.cpp=.opp) $(CPPGIMI:.cpp=.opp)

OBJ_LOG_CPP=$(SRC_LOG:.cpp=.opp)
OBJ_LOG_C=$(CSRC_LOG:.c=.o)

OBJ_OCCU_CPP=$(SRC_OCCU_SLAM:.cpp=.opp)
OBJ_OCCU_C=$(CSRC_OCCU_SLAM:.c=.o)

OBJ_LASER_ODO=$(SRC_LASER_ODO:.cpp=.opp)
OBJ_LASER_ODO_C=$(CSRC_LASER_ODO:.c=.o)

OBJ_OCCU_MCL_CPP=$(SRC_OCCU_MCL:.cpp=.opp)
OBJ_OCCU_MCL_C=$(CSRC_OCCU_MCL:.c=.o)

OBJ_OCCU_J2B2_TEST_CPP=$(SRC_OCCU_J2B2_TEST:.cpp=.opp)
OBJ_OCCU_J2B2_TEST_C=$(CSRC_OCCU_J2B2_TEST:.c=.o)

OBJ_OCCU_SLAMCL_CPP=$(SRC_OCCU_SLAMCL:.cpp=.opp)
OBJ_OCCU_SLAMCL_C=$(CSRC_OCCU_SLAMCL:.c=.o)

OBJ_TOPO_CPP=$(SRC_TOPO:.cpp=.opp)
OBJ_TOPO_C=$(CSRC_TOPO:.c=.o)

OBJ_PLOT_DR_CPP=$(SRC_PLOT_DR:.cpp=.opp)
OBJ_PLOT_DR_C=$(CSRC_PLOT_DR:.c=.o)

OBJ_TIMING_TEST_CPP=$(SRC_TIMING_TEST:.cpp=.opp)
OBJ_TIMING_TEST_C=$(CSRC_TIMING_TEST:.c=.o)

## Define compiles and links
COMPILEC=$(CC) $(CFLAGS) $(CINCLUDES) -c
COMPILECPP=$(CXX) $(CPPFLAGS) $(CPPINCLUDES) -c
LINK=$(LD) $(LDFLAGS) $(LIBS)

.PHONY: all clean

all: topo j2b2 occu_slam occu_mcl occu_slamcl j2b2_test laser_odo logconvert plot_dr timing_test

j2b2: $(OBJSC) $(OBJSCPP)
			$(LINK) -o $@ $(OBJSC) $(OBJSCPP)
		
logconvert: $(OBJ_LOG_CPP) $(OBJ_LOG_C)
						$(LINK) -o $@ $(OBJ_LOG_C) $(OBJ_LOG_CPP)
			
occu_slam: $(OBJ_OCCU_C) $(OBJ_OCCU_CPP)
						$(LINK) -o $@ $(OBJ_OCCU_C) $(OBJ_OCCU_CPP)
						
occu_mcl: $(OBJ_OCCU_MCL_C) $(OBJ_OCCU_MCL_CPP)
						$(LINK) -o $@ $(OBJ_OCCU_MCL_C) $(OBJ_OCCU_MCL_CPP)

occu_slamcl: $(OBJ_OCCU_SLAMCL_C) $(OBJ_OCCU_SLAMCL_CPP)
						$(LINK) -o $@ $(OBJ_OCCU_SLAMCL_C) $(OBJ_OCCU_SLAMCL_CPP)

j2b2_test: $(OBJ_OCCU_J2B2_TEST_C) $(OBJ_OCCU_J2B2_TEST_CPP)
						$(LINK) -o $@ $(OBJ_OCCU_J2B2_TEST_C) $(OBJ_OCCU_J2B2_TEST_CPP)
						
topo: $(OBJ_TOPO_C) $(OBJ_TOPO_CPP)
						$(LINK) -o $@ $(OBJ_TOPO_C) $(OBJ_TOPO_CPP)

plot_dr: $(OBJ_PLOT_DR_C) $(OBJ_PLOT_DR_CPP)
	$(LINK) -o $@ $(OBJ_PLOT_DR_C) $(OBJ_PLOT_DR_CPP)

laser_odo: $(OBJ_LASER_ODO) $(OBJ_LASER_ODO_C)
						$(LINK) -o $@ $(OBJ_LASER_ODO_C) $(OBJ_LASER_ODO)
						
timing_test: $(OBJ_TIMING_TEST_CPP) $(OBJ_TIMING_TEST_C)
						$(LINK) -o $@ $(OBJ_TIMING_TEST_C) $(OBJ_TIMING_TEST_CPP)
						
clean:
	rm -f $(OBJSC) $(OBJSCPP) $(OBJ_OCCU_MCL_C) $(OBJ_OCCU_MCL_CPP) $(OBJ_OCCU_CPP) \
		 $(OBJ_OCCU_C) $(OBJ_OCCU_SLAMCL_CPP) $(OBJ_OCCU_SLAMCL_C) j2b2 occu_slam occu_mcl *.out *~ *.d *.dpp

## Compile rule for all .o (.c) file
%.o:    %.c
				$(COMPILEC) -o $@ $<

## Compile rule for all .opp (.cpp) files
%.opp:	%.cpp
				$(COMPILECPP) -o $@ $<

## Dependency generation rule: For all required '.d' files
#%.d:	  %.c
#				$(CC) -MM $(CFLAGS) $< > $@.$$$$; \
#				sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
#				rm -f $@.$$$$
#
## Dependency generation rule: For all required '.dpp' files
#%.dpp:	%.cpp
#				$(CXX) -MM $(CPPFLAGS) $< > $@.$$$$; \
#				sed 's,\($*\)\.o[ :]*,\1.opp $@ : ,g' < $@.$$$$ > $@; \
#				rm -f $@.$$$$
#
#%.h:	%.h
#
# Include dependency rules
#-include $(SRCSCPP:.cpp=.dpp) $(CPPGIMI:.cpp=.dpp)
#-include $(SRCSC:.c=.d) $(CGIMI:.c=.d)
